<template>
  <div class="article-detail">
    <div class="article-header">
      <h1>{{ article.title }}</h1>
      <div class="article-meta">
        <span class="date">{{ article.date }}</span>
        <div class="tags">
          <span v-for="tag in article.tags"
                :key="tag"
                class="tag"
                @click="handleTagClick(tag)">
            #{{ tag }}
          </span>
        </div>
      </div>
    </div>

    <div class="article-content"
         v-html="article.content"></div>

    <div class="article-footer">
      <div class="share-buttons">
        <button @click="shareArticle('twitter')">分享到 Twitter</button>
        <button @click="shareArticle('facebook')">分享到 Facebook</button>
      </div>

      <div class="comments-section">
        <h3>评论</h3>
        <div class="comment-form">
          <textarea v-model="newComment"
                    placeholder="写下你的评论..."></textarea>
          <button @click="submitComment">提交评论</button>
        </div>

        <div class="comments-list">
          <div v-for="comment in comments"
               :key="comment.id"
               class="comment">
            <div class="comment-header">
              <span class="comment-author">{{ comment.author }}</span>
              <span class="comment-date">{{ comment.date }}</span>
            </div>
            <p class="comment-content">{{ comment.content }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';

const route = useRoute();
const router = useRouter();
const article = ref({});
const comments = ref([]);
const newComment = ref('');

// 模拟文章数据
const fetchArticle = async () => {
  // 这里应该是从后端API获取数据
  article.value = {
    id: route.params.id,
    title: '示例文章标题',
    date: '2024-03-20',
    tags: ['Vue', 'JavaScript', '前端'],
    content: `
      <h2>引言</h2>
      <p>这是一篇示例文章的内容...</p>
      <h2>正文</h2>
      <p>更多内容...</p>
    `
  };
};

const handleTagClick = (tag) => {
  router.push({
    path: '/tags',
    query: { tag }
  });
};

const shareArticle = (platform) => {
  // 实现分享功能
  const url = window.location.href;
  const title = article.value.title;
  
  if (platform === 'twitter') {
    window.open(`https://twitter.com/intent/tweet?text=${title}&url=${url}`);
  } else if (platform === 'facebook') {
    window.open(`https://www.facebook.com/sharer/sharer.php?u=${url}`);
  }
};

const submitComment = () => {
  if (!newComment.value.trim()) return;
  
  comments.value.unshift({
    id: Date.now(),
    author: '访客',
    date: new Date().toLocaleDateString(),
    content: newComment.value
  });
  
  newComment.value = '';
};

onMounted(() => {
  fetchArticle();
});
</script>

<style scoped>
.article-detail {
  max-width: 800px;
  margin: 100px auto;
  padding: 2rem;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 15px;
  color: #fff;
}

.article-header {
  margin-bottom: 2rem;
}

.article-header h1 {
  font-size: 2.5rem;
  margin-bottom: 1rem;
  background: linear-gradient(45deg, #fff, #88ccff);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.article-meta {
  display: flex;
  gap: 1rem;
  align-items: center;
  color: rgba(255, 255, 255, 0.7);
}

.tags {
  display: flex;
  gap: 0.5rem;
}

.tag {
  padding: 0.2rem 0.5rem;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 15px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.tag:hover {
  background: rgba(255, 255, 255, 0.2);
}

.article-content {
  line-height: 1.8;
  margin-bottom: 3rem;
}

.article-footer {
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  padding-top: 2rem;
}

.share-buttons {
  display: flex;
  gap: 1rem;
  margin-bottom: 2rem;
}

.share-buttons button {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  cursor: pointer;
  transition: all 0.3s ease;
}

.share-buttons button:hover {
  background: rgba(255, 255, 255, 0.2);
}

.comments-section {
  margin-top: 2rem;
}

.comment-form textarea {
  width: 100%;
  padding: 1rem;
  margin-bottom: 1rem;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  border-radius: 10px;
  color: #fff;
  resize: vertical;
  min-height: 100px;
}

.comment-form button {
  padding: 0.5rem 2rem;
  background: #88ccff;
  border: none;
  border-radius: 20px;
  color: #000;
  cursor: pointer;
  transition: all 0.3s ease;
}

.comment {
  margin-top: 2rem;
  padding: 1rem;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 10px;
}

.comment-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 0.5rem;
  color: rgba(255, 255, 255, 0.7);
}

@media (max-width: 768px) {
  .article-detail {
    margin: 80px 1rem;
    padding: 1rem;
  }
  
  .article-header h1 {
    font-size: 2rem;
  }
}
</style> 